package com.orangelife.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ListSetUtils {

	/****
	 * list 转set
	 * 
	 * @param tar
	 * @return
	 */
	public <T> Set<T> listToSet(List<T> tar) {
		Set<T> set = new HashSet<T>();

		set.addAll(tar);
		return set;

	}

	/***
	 * set 转list
	 * 
	 * @param tar
	 * @return
	 */

	public <T> List<T> setToList(Set<T> tar) {
		List<T> list = new ArrayList<T>();

		list.addAll(tar);
		return list;

	}

	/***
	 * list拆分为多个list
	 * 
	 * @param resList
	 * @param count
	 * @return
	 */
	public <T> List<List<T>> splitList(List<T> resList, int count) {
		if (resList == null || count < 1) {
			return null;
		}
		List<List<T>> ret = new ArrayList<List<T>>();
		int size = resList.size();
		if (size <= count) { // 数据量不足count指定的大小
			ret.add(resList);
		} else {
			int pre = size / count;
			int last = size % count;
			// 前面pre个集合，每个大小都是count个元素
			for (int i = 0; i < pre; i++) {
				List<T> itemList = new ArrayList<T>();
				for (int j = 0; j < count; j++) {
					itemList.add(resList.get(i * count + j));
				}
				ret.add(itemList);
			}
			// last的进行处理
			if (last > 0) {
				List<T> itemList = new ArrayList<T>();
				for (int i = 0; i < last; i++) {
					itemList.add(resList.get(pre * count + i));
				}
				ret.add(itemList);
			}
		}
		return ret;
	}

	public <T> void print(List<List<T>> list) {
		for (int i = 0; i < list.size(); i++) {
			Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {

				}
			});
			thread.start();

		}
	}

	class HandleThread<T> extends Thread implements Runnable {
		private String threadName;
		private List<T> data;
		private int start;
		private int end;

		public HandleThread(String threadName, List<T> data) {
			this.threadName = threadName;
			this.data = data;
		}

		public void run() {
			for (int i = 0; i < data.size(); i++) {
				System.out.println(threadName + "                            "
						+ data.get(i).toString());
			}

		}

	}

	public static void main(String[] args) {
		ListSetUtils listSetUtils = new ListSetUtils();
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < 10000; i++) {
			String id = IdUtil.getuuId();
			list.add(id);
			// System.out.println(id);
		}
		List<List<String>> li = listSetUtils.splitList(list, 1000);
		for (int i = 0; i < li.size(); i++) {
			HandleThread<String> thread = listSetUtils.new HandleThread<String>(
					"线程[" + (i) + "] ", li.get(i));
			thread.start();

		}

	}
}
